/*
 * @文件相对于项目的路径: /htxincailiao-webapp/src/store/modules/industry.js
 * @message:
 */
import { getIndustryCategoryListRequest, getIndustryTagListRequest, getAllianceMemberListRequest } from "@/api/industry";

const industry = {
  namespaced: true,
  state: {
    categories: [],
    tags: [],

    moduleCategories: [
      {
        name: "会议",
      },
      {
        name: "会展",
      },
      {
        name: "沙龙",
      },
      {
        name: "产业联盟",
      },
    ],

    filterCategories: [
      {
        name: "全部",
      },
      {
        name: "近期",
      },
      {
        name: "往期",
      },
      {
        name: "进行中",
      },

      {
        name: "我参与的",
      },
    ],

    meetings: [],
    meetingInfo: [],
    exhibitions: [],
    exhibitionInfo: [],
    salons: [],
    salonInfo: [],
    allianceMembers: [],
    paymentId: undefined,
    pageSize: 20,
    currentPage: 1,
    originalPage: 1,
    pageTotal: 0,
    isHasMore: true,
  },

  mutations: {
    SET_CATEGORIES: (state, categories) => {
      state.categories = categories;
    },

    SET_TAGS: (state, tags) => {
      state.tags = tags;
    },

    SET_IS_HAS_MORE: (state, isHasMore) => {
      state.isHasMore = isHasMore;
    },

    SET_EXHIBITIONS: (state, exhibitions) => {
      state.exhibitions = exhibitions;
    },

    SET_EXHIBITION_INFO: (state, exhibitionInfo) => {
      state.exhibitionInfo = exhibitionInfo;
    },

    SET_SALONS: (state, salons) => {
      state.salons = salons;
    },

    SET_SALON_INFO: (state, salonInfo) => {
      state.salonInfo = salonInfo;
    },

    SET_ALLIANC_MEMBERS: (state, allianceMembers) => {
      state.allianceMembers = allianceMembers;
    },
    SET_PAYMENT_ID: (state, paymentId) => {
      state.paymentId = paymentId;
    },
    SET_CURRENT_PAGE: (state, currentPage) => {
      if (!currentPage) {
        currentPage = 1;
      }
      state.currentPage = currentPage;
    },

    SET_ORIGINAL_PAGE: (state, originalPage) => {
      if (!originalPage) {
        originalPage = 1;
      }
      state.originalPage = originalPage;
    },

    SET_PAGE_TOTAL: (state, pageTotal) => {
      state.pageTotal = pageTotal;
    },
  },

  actions: {
    getIndustryCategoryListAction({ commit }, parameters) {
      return new Promise((resolve, reject) => {
        getIndustryCategoryListRequest(parameters)
          .then((response) => {
            if (response && response.success && response.result.records) {
              const data = response.result.records;
              commit("SET_CATEGORIES", data);
              resolve(response);
            } else {
              resolve(response);
            }
          })
          .catch((error) => {
            reject(error);
          });
      });
    },

    getIndustryTagListAction({ commit }, parameters) {
      return new Promise((resolve, reject) => {
        getIndustryTagListRequest(parameters)
          .then((response) => {
            if (response && response.success && response.result.records) {
              const data = response.result.records;
              commit("SET_TAGS", data);
              resolve(response);
            } else {
              resolve(response);
            }
          })
          .catch((error) => {
            reject(error);
          });
      });
    },

    getAllianceMemberListAction({ state, commit }, { isRefresh, parameters }) {
      if (isRefresh) {
        commit("SET_IS_HAS_MORE", false);
        commit("SET_CURRENT_PAGE", 1);
      }
      parameters = {
        ...parameters,
        pageNo: state.currentPage,
        pageSize: state.pageSize,
      };
      return new Promise((resolve, reject) => {
        getAllianceMemberListRequest(parameters)
          .then((response) => {
            if (response && response.result && response.result.records) {
              if (isRefresh) {
                commit("SET_ALLIANC_MEMBERS", []);
              }
              const items = response.result.records;
              const pages = response.result.pages;
              var current = state.currentPage;
              if (current <= pages) {
                const concatedItems = state.items.concat(items);
                commit("SET_PAGE_TOTAL", pages);
                commit("SET_ALLIANC_MEMBERS", concatedItems);

                current += 1;
                commit("SET_CURRENT_PAGE", current);
              }
              commit("SET_IS_HAS_MORE", current <= pages);

              resolve(response);
            } else {
              reject(response);
            }
          })
          .catch((error) => {
            reject(error);
          });
      });
    },

    updateCurrentPageAction({ commit }, payload) {
      commit("SET_CURRENT_PAGE", payload);
    },

    updateOriginalPageAction({ commit }, payload) {
      commit("SET_ORIGINAL_PAGE", payload);
    },
  },
};

export default industry;
